package spring_learn.a01;


import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultSingletonBeanRegistry;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.io.Resource;

import java.lang.reflect.Field;
import java.sql.SQLOutput;
import java.util.Locale;
import java.util.Map;

/**
 * BeanFactory和ApplicationContext的区别
 *
 * 1：看这两个类的类图，bean是applicaitonconext的父接口
 */
@SpringBootApplication
public class A01 {


    public static void main(String[] args) throws Exception {
        ConfigurableApplicationContext run = SpringApplication.run(A01.class);
        System.out.println(run.getClass());

        /*
          AnnotationConfigServletWebServerApplicationContext父类是GenericeApplicaitonContext
          父类中存在一个DefaultSingletonBeanFactory
          DefaultSingletonBeanFactory父类是DefaultSingletonBeanRegistry
          而DefaultSingletonBeanRegistry有一个singletonObjects属性，是一个hashmap，key是bean的名称，value是具体对象
         */
        /*Field singletonObjects = DefaultSingletonBeanRegistry.class.getDeclaredField("singletonObjects");
        singletonObjects.setAccessible(true);
        ConfigurableListableBeanFactory beanFactory = run.getBeanFactory();
        Map<String,Object> o = (Map<String,Object>)singletonObjects.get(beanFactory);
        o.entrySet().stream().filter(entry->entry.getKey().startsWith("component")).forEach(e->{
            System.out.println(e.getKey()+"---"+e.getValue());
        });*/


        /*
        applicationContext比beanfactory多出来的功能
        分别对应多出来的四个接口：具体看类图
         */

        //1:message source
        System.out.println(run.getMessage("hi",null, Locale.CHINA));
        System.out.println(run.getMessage("hi",null, Locale.ENGLISH));

        //2:resouce pattern resolver
        Resource[] resources = run.getResources("classpath:messages*.properties");
        for (Resource resource:resources){
            System.out.println(resource);
        }

        //Environment Capable
        String java_home = run.getEnvironment().getProperty("server.port");
        System.out.println(java_home);


        //ApplicationEventPublisher
        UserService bean = run.getBean(UserService.class);
        bean.registerUser();
    }
}
